# -*- coding: utf-8 -*-
"""
Created on Tue Jul 10 12:20:28 2012

@author: del.l
"""
import numpy as np

class EDF:
    def __init__(self,path):
        with open(path) as f:
            names = ['vf',# version of data
            'lpi',# local patient identificantion
            'lri',# Local recordign identification
            'sdr',# startdate
            'str',# starttime
            'nbhr',# number of bytes in header
            'r0',# reserved
            'ndr',#number of data records
            'ddr',# duration of the data records
            'nsr'] # number of signals in data record
            l = [8,80,80,8,8,8,44,8,8,4]
            for i in xrange(len(names)): self.__dict__[names[i]] = f.read(l[i])
            # Read the rest of the headers
            names = ['l',# Label
            't',# Tranducer type
            'pd',# Physicial dimension
            'pmin',# Physical min
            'pmax',# Physical max
            'dmin',# Digital min
            'dmax',# Digital max
            'pf', # Prefiltering
            'ns', # number of samples
            'r1'] # Reserved 
            for i in names: self.__dict__[i] = []          
            l = [16,80,8,8,8,8,8,80,8,32]
            
            for i in xrange(int(self.ndr)):
                self.__dict__[names[i]] += [f.read(l[i])]
            
            self.recs = []
                            
            if int(self.ndr) == 1:
                self.recs += [np.frombuffer(f,dtype=np.int,count=-1,offset=512)]
            elif int(self.ndr) > 1:
                null = int(self.ndr) * 256
                for i in xrange(int(self.ndr)):
                    dp = int(self.ns[i])*int(self.ndr)
                    self.recs += [np.frombuffer(f,dtype=np.int32,count=dp,offset=null)]
                    null += dp



if __name__ == '__main__':
    import os
    f = EDF(os.path.join('..','data','s1e3b1t3.edf'))
        
        